from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSTargetting as GSHandler
from GSResultsPolicies import GSMission as GSResults

from GSTargetInfoPolicies import GSSquarePlotTarget

from CivcraftPolicies import CivcraftSpellPlotGraphicsPolicy as GSPlot

import GerikSpellsUtils

from CivcraftSCUtils import checkAndRemoveEnergy
from CivcraftSCUtils import showNotEnoughEnergyMsg
from CivcraftSCUtils import getEnergyUsageString
from CivcraftSCUtils import isHallucination

gc = CyGlobalContext()
localtext = CyTranslator()

class CorsairBase(GerikSpellBase, GSGroup, GSHandler):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)


        self.m_iCorsairType = gc.getInfoTypeForString("UNIT_CORSAIR")
            
    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not pCastingUnit.getUnitType() == self.m_iCorsairType:
            return False

        if isHallucination(pCastingUnit):
            return False

        # Show this for all Corsairs
        if bVisible:
            return True

        return True

class DisruptionWeb(CorsairBase, GSResults):
    def __init__(self):
        CorsairBase.__init__(self)

        # Constants
        self.m_iDisruptionWebEnergyCost = 150
        self.m_iDisruptionWebRange = 5
        self.m_iDisruptionWebSize = 2
        self.m_iDisruptionWebPlotAttachment = gc.getInfoTypeForString("PLOT_ATTACHMENT_DISRUPTION_WEB")

        self.m_iResearchNeeded = gc.getInfoTypeForString("PROJECT_PROTOSS_CORSAIR_DISRUPTION_WEB")
        self.m_iResearchBuildingToBuildAt = gc.getProjectInfo(self.m_iResearchNeeded).getBuildingToBuildAt()
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iResearchBuildingToBuildAt).getDescription()) )

        # Policies
        pPlotTargetPolicy = GSSquarePlotTarget(self.canTargetPlot, self.m_iDisruptionWebSize)
        GSResults.__init__(self, pPlotTargetPolicy)

    def getHelpText(self, pGroup, pPlot):
        szText = getEnergyUsageString(self.m_iDisruptionWebEnergyCost)

        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            szText += self.m_szNotValidText
        else:
            szText += gc.getSpellInfo(self.getSpellNumber()).getHelp()

        return szText

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not CorsairBase.canUnitCast(self, pCastingUnit, pPlot, bVisible):
            return False

        if bVisible:
            return True

        # Check if they have the research
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iResearchNeeded ):
            return False

        return True

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pTargetPlot, bVisible):
        return True

    def getRangePlotList(self, pCastingPlot):
        return GerikSpellsUtils.getRangePlotDistanceList(pCastingPlot, self.m_iDisruptionWebRange)

    def createTargettingStageList(self, bAlt, bShift, bCtrl):
        pStageList = []

        pPlotTargetPolicy = GSSquarePlotTarget(self.canTargetPlot, self.m_iDisruptionWebSize)
        pPlotTargetPolicy.setRangeMakerCallback(self.getRangePlotList)
        pPlotTargetPolicy.setCheckRange(True)

        pPlotGraphicsPolicy = GSPlot()
        pPlotGraphicsPolicy.setShowRangeGraphics(True)

        return [ (pPlotTargetPolicy, pPlotGraphicsPolicy) ]

    def startSpellResults(self, pCastingGroup, pTargetInfo):
        bShowNotEnoughEnergy = False
        
        iPlayer = pCastingGroup.getOwner()
        for pUnit in pCastingGroup.getUnits():
            if checkAndRemoveEnergy(pUnit, self.m_iDisruptionWebEnergyCost):
                for pLoopPlot in pTargetInfo.getAffectedTargetList(pCastingGroup, pCastingGroup.plot()):
                    pLoopPlot.attachPlotAttachment(iPlayer, self.m_iDisruptionWebPlotAttachment)
            else:
                bShowNotEnoughEnergy = True

        if bShowNotEnoughEnergy:
            showNotEnoughEnergyMsg(iPlayer)
                    
            
            
